//
//  CustomerButton.m
//  PickerDemo
//
//  Created by Cluy on 16/12/20.
//  Copyright © 2016年 Cluy. All rights reserved.
//

#import "CustomerButton.h"
#import "CustomerPickerView.h"

@implementation CustomerButton
- (void)awakeFromNib
{
    [super awakeFromNib];
    [self initView];
}
- (BOOL)isUserInteractionEnabled
{
    return YES;
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}
- (void) initView
{
    //如果引入了IQKeyboard 做处理
//    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
//    manager.enableAutoToolbar = NO;
    
    NSLog(@"%f",self.frame.size.width);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchAction)];
    [tap setNumberOfTouchesRequired:1];
    [tap setNumberOfTapsRequired:1];
    [self addGestureRecognizer:tap];
    _backImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    _backImageView.image = [UIImage imageNamed:@"select_button_bg.png"];
    [self addSubview:_backImageView];
    _title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width-30, self.frame.size.height)];
    _title.textAlignment = NSTextAlignmentCenter;
    _title.textColor = [UIColor blackColor];
    _title.font = [UIFont systemFontOfSize:13.0];
    [self addSubview:_title];
    _backImageView.hidden = YES;
    _title.hidden = YES;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    _title.frame = CGRectMake(3, 0, self.frame.size.width-33, self.frame.size.height);
    _backImageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}
-(void)setShowBackImg:(BOOL)showBackImg{
    if (showBackImg == YES) {
        _backImageView.hidden = NO;
        _title.hidden = NO;
    }
    
}
- (void)touchAction
{
    if (_itemList) {
        [self resignFirstResponder];
        CustomerPickerView *customerPicker = [[CustomerPickerView alloc]initWithColumn:_itemList titleKey:_titleKey];
        self.inputView = customerPicker;
        [self becomeFirstResponder];
        customerPicker.didSelectAction = ^(NSArray *selectItems){
            NSLog(@"值为：%@",selectItems);
            if (_selectItemValue) {
                _selectItemValue(selectItems);
            }
            [self resignFirstResponder];
        };
        customerPicker.didCancelAction = ^(){
            [self resignFirstResponder];
        };
    }else{
        _notDataAction();
    }
    
}
@end
